package models;

import javax.persistence.*;

import com.avaje.ebean.Page;
import com.avaje.ebean.validation.NotNull;

import play.db.ebean.Model;
import play.db.ebean.Model.Finder;

@Entity
public class ForestrySubType extends Model{

	@Id
	public Long id;
	
	@NotNull
	public String forestSubTypeKey;
	
	@ManyToOne
	@NotNull
	public ForestryType forestType;
	
	//Finder
	public static Finder<Long, ForestrySubType> find = new Finder<Long, ForestrySubType>(
			Long.class, ForestrySubType.class);
	
	public static ForestrySubType getForestSubType(ForestrySubType forestrySubType) {
		return find.fetch("forestrySubType").where().idEq(forestrySubType.id).findUnique();
	}
	
    /**
     * Return a page of ForestrySubType
     *
     * @param page Page to display
     * @param pageSize Number of computers per page
     * @param sortBy Computer property used for sorting
     * @param order Sort order (either or asc or desc)
     * @param filter Filter applied on the name column
     */
    public static Page<ForestrySubType> page(int page, int pageSize, String sortBy, String order, String filter) {
        return 
            find.where()
                .ilike("name", "%" + filter + "%")
                .orderBy(sortBy + " " + order)
                .fetch("forestType")
                .findPagingList(pageSize)
                .getPage(page);
    }
}
